#include "homepage.h"
#include "ui_homepage.h"
#include "globalval.h"

HomePage::HomePage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::HomePage)
{
    ui->setupUi(this);

    //mqtt相关
    MqttHandler *mqtt = new MqttHandler;
    GlobalVal::mqtt = mqtt;
    mqtt->setHost(QHostAddress("192.168.31.160"));
    mqtt->setPort(1883);
    mqtt->connectToHost();

    //实例化四个界面
    m0Wig = new M0Wig;
    modbusWig = new ModbusWig;
    mbappWig = new MbappWig;
    setWig = new SetWig;

    //将四个界面依次添加到堆栈窗体
    ui->stackedWidget->addWidget(m0Wig);
    ui->stackedWidget->addWidget(modbusWig);
    ui->stackedWidget->addWidget(mbappWig);
    ui->stackedWidget->addWidget(setWig);


    //连接列表界面和堆栈窗体的信号
    QObject::connect(ui->listWidget, SIGNAL(currentRowChanged(int)),
                     ui->stackedWidget, SLOT(setCurrentIndex(int)));



    //哪个界面需要接收Mqtt的消息，就订阅这个信号，然后在自己的界面文件中处理消息
    QObject::connect(mqtt, SIGNAL(dataUpSignal(QByteArray)),
                     this->m0Wig, SLOT(dataUpSlot(QByteArray)));
    QObject::connect(mqtt, SIGNAL(dataUpSignal(QByteArray)),
                     this->modbusWig, SLOT(dataUpSlot(QByteArray)));
    connect(this,SIGNAL(ctrll()),this,SLOT(mySlot()));//变化信号
}

HomePage::~HomePage()
{
    delete ui;
}

void HomePage::mySlot()
{
    qDebug()<<"mssssect error";
}

void HomePage::dataUpSlot(QByteArray  message)
{
      qDebug()<<QString(message);


}
